# -*- coding: utf-8 -*-
# @author: 龙非池
from __future__ import unicode_literals

from django.db import models

from wlmodels.codes.dicts import ca22_dict, ca24_dict, ca50_dict, ca80_dict, cb16_dict, cd12_dict, cz01_dict, cz04_dict, ex08_dict, ex14_dict
from wlmodels.codes.fields import CrossForeignCharField
from wlmodels.codes.models import ELBaseModel, ELCA02, ELCA04, EntIdable


# 投资信息
class ELInv(ELBaseModel):
    entobj = models.ForeignKey('company.ELPost', verbose_name='所属企业', db_column='entid', default=0,
                               related_name='c_inv', to_field='entid')
    personid = models.ForeignKey('company.ELPerson', verbose_name='关联个人', db_column='personid', null=True, blank=True,
                                 related_name='p_inv', to_field='personid', default=0)
    entid_inv = models.IntegerField('关联企业id', null=True, blank=True, default=0)
    INV = models.CharField('股东名称', max_length=150, null=True, blank=True, db_index=True)
    INVTYPE = models.CharField('投资人类型', max_length=2, null=True, blank=True, choices=ca24_dict)
    BLICNO = models.CharField('证照号码', max_length=30, null=True, blank=True)
    BLICTYPE = models.CharField('证照类型', max_length=2, null=True, blank=True, choices=ca50_dict)
    CERNO = models.CharField('证件号码', max_length=30, null=True, blank=True, db_index=True)
    CERTYPE = models.CharField('证件类型', max_length=2, null=True, blank=True, choices=cb16_dict)
    COUNTRY_id = CrossForeignCharField('国别(地区)', max_length=3, null=True, blank=True, db_column='COUNTRY')
    CURRENCY_id = CrossForeignCharField('币种', max_length=3, null=True, blank=True, db_column='CURRENCY')
    SUBCONAM = models.FloatField('认缴出资额', null=True, blank=True)
    ACCONAM = models.FloatField('实缴出资额', null=True, blank=True)
    SUBCONAMUSD = models.FloatField('认缴出资额折万美元', null=True, blank=True)
    ACCONAMUSD = models.FloatField('实缴出资额折万美元', null=True, blank=True)
    CONAM = models.FloatField('出资额', null=True, blank=True)
    CONPROP = models.FloatField('出资比例', null=True, blank=True)
    CONFORM = models.CharField('出资方式', max_length=2, null=True, blank=True, choices=ca22_dict)
    CONDATE = models.DateField('出资日期', null=True, blank=True)
    BALDELPER = models.DateField('余额缴付期限', null=True, blank=True)
    EXEAFFSIGN = models.CharField('执行合伙事务标志', max_length=1, null=True, blank=True, choices=ex14_dict)
    updated = models.DateTimeField('更新时间', null=True, blank=True)

    class Meta:
        verbose_name = '企业投资信息'
        verbose_name_plural = '企业投资信息'
        app_label = "company"
        db_table = 'company_inv'

    def _entid(self):
        from wlmodels.company.models import ELPost
        entid = ELPost.objects.filter(entid=self.entobj_id).first()
        if entid:
            return entid
        return ""

    _entid.short_description = "所属企业"

    entid = property(_entid)

    def _entinv(self):
        from wlmodels.company.models import ELPost
        entinv = ELPost.objects.filter(entid=self.entid_inv).first()
        if entinv:
            return entinv
        return ""

    _entinv.short_description = "所属企业"

    entinv = property(_entinv)

    def __unicode__(self):
        return u'%s的股东信息' % self.entid

    def _country(self):
        the_country = list(ELCA02.objects.filter(code=self.COUNTRY_id).values_list("name", flat=True))
        if len(the_country):
            return the_country[0]
        return ""

    _country.short_description = "国别(地区)"

    COUNTRY = property(_country)

    def _currency(self):
        the_currency = list(ELCA04.objects.filter(code=self.CURRENCY_id).values_list("name", flat=True))
        if len(the_currency):
            return the_currency[0]
        return ""

    _currency.short_description = "币种"

    CURRENCY = property(_currency)


# 股权出质记录
class ELStockImpawn(EntIdable):
    IMPORG_ID = models.CharField('质押ID', max_length=50, null=True, blank=True, help_text='在省局采集时自动生成或采集流水号')
    IMPORG = models.CharField('质权人', max_length=100, null=True, blank=True)
    IMPORGTYPE = models.CharField('质权人类别', max_length=2, null=True, blank=True, choices=ca80_dict)
    IMPAM = models.FloatField('质押金额', null=True, blank=True)
    IMPONRECDATE = models.DateField('质押备案日期', null=True, blank=True)
    IMPEXAEEP = models.CharField('质押审批部门', max_length=100, null=True, blank=True)
    IMPSANDATE = models.DateField('质押批准日期', null=True, blank=True)
    IMPTO = models.DateField('质押截至日期', null=True, blank=True)
    EXESTATE = models.CharField('执行状态', max_length=1, null=True, blank=True, choices=cz01_dict)
    updated = models.DateTimeField('更新时间', null=True, blank=True)

    class Meta:
        verbose_name = '股权出质记录'
        verbose_name_plural = '股权出质记录'
        app_label = "companyplus"
        db_table = 'company_stock_impawn'

    def __unicode__(self):
        return u'%s的股权出质' % self.entid


# 股权冻结历史
class ELStockFrost(EntIdable):
    FRO_ID = models.CharField('冻结ID', max_length=50, null=True, blank=True, help_text='存放冻结文号或流水号或自动生成')
    FROAM = models.FloatField('冻结金额', null=True, blank=True)
    SHARFROPROP = models.FloatField('股权冻结比例', null=True, blank=True)
    FROAUTH = models.CharField('冻结机关', max_length=200, null=True, blank=True)
    FROFROM = models.DateField('冻结起始日期', null=True, blank=True)
    FROTO = models.DateField('冻结截至日期', null=True, blank=True)
    FRODOCNO = models.CharField('冻结文号', max_length=60, null=True, blank=True)
    FROREASON = models.TextField('冻结原因', null=True, blank=True, help_text='不是标准指标，根据广东实际情况增加')
    EXESTATE = models.CharField('执行状态', max_length=1, null=True, blank=True, choices=cz01_dict)
    CORENTNAME = models.CharField('相关企业名称', max_length=150, null=True, blank=True)
    THAWAUTH = models.CharField('解冻机关', max_length=200, null=True, blank=True)
    THAWDOCNO = models.CharField('解冻文号', max_length=60, null=True, blank=True)
    THAWDATE = models.DateField('解冻日期', null=True, blank=True)
    FROZSIGN = models.CharField('冻结标志', max_length=1, null=True, blank=True, choices=cz04_dict)
    THAWCOMMENT = models.TextField('解冻说明', null=True, blank=True, help_text='不是标准指标，根据广东实际情况增加')
    updated = models.DateTimeField('更新时间', null=True, blank=True)

    class Meta:
        verbose_name = '股权冻结历史'
        verbose_name_plural = '股权冻结历史'
        app_label = "companyplus"
        db_table = 'company_stock_frost'

    def __unicode__(self):
        return u'%s的股权冻结历史' % self.entid


# 企业最终控股股东信息
class ELFinalShareHolder(EntIdable):
    FINALENTNAME = models.CharField("最终控股股东名称", max_length=128, null=True, blank=True)
    FINALCOUNTRY = models.CharField("国别", max_length=10, null=True, blank=True)
    FINALINVTYPE = models.CharField("股东类别", max_length=20, null=True, blank=True)
    FINALSUBCONAM = models.CharField("认缴出资（万）", max_length=20, null=True, blank=True)
    FINALCURRENCY = models.CharField("出资币种", max_length=10, null=True, blank=True)
    FINALACCONAM = models.CharField("实缴出资（万）", max_length=20, null=True, blank=True)
    FINALCONAM = models.CharField("出资额（万）", max_length=20, null=True, blank=True)
    FINALCONFORM = models.CharField("出资方式", max_length=10, null=True, blank=True)
    FINALCONDATE = models.CharField("出资日期", max_length=20, null=True, blank=True)
    FINALRATIO = models.CharField("出资比例", max_length=20, null=True, blank=True)
    CAPITALCHAINEX = models.TextField("出资链", null=True, blank=True)

    class Meta:
        verbose_name = '企业最终股东'
        verbose_name_plural = '企业最终股东'
        app_label = "companyplus"
        db_table = 'company_stock_final'

    def __unicode__(self):
        return u'%s-最终股东' % self.entid


# 分期实缴信息
class ELStockContriFact(EntIdable):
    personid = models.IntegerField('关联个人id', null=True, blank=True, default=0)
    entid_inv = models.IntegerField('关联企业id', null=True, blank=True, default=0)
    FC_ID = models.CharField('实缴ID', max_length=50, null=True, blank=True)
    CONTERMNO = models.IntegerField('出资期次', null=True, blank=True)
    CURACTCONAM = models.DecimalField('本期实缴出资额', max_digits=15, decimal_places=2, null=True, blank=True, help_text='万元')
    CURACTCONAMUSD = models.DecimalField('本期实缴出资额折万美元', max_digits=15, decimal_places=2, null=True, blank=True, help_text='万美元')
    CURRENCY_id = CrossForeignCharField('币种', max_length=3, null=True, blank=True, db_column='CURRENCY')
    CONFORM = models.CharField('出资方式', max_length=2, null=True, blank=True, choices=ca22_dict)
    CONSOURCE = models.CharField('出资来源', max_length=2, null=True, blank=True, choices=ex08_dict)
    POSDUEREA = models.TextField('欠资原因', null=True, blank=True)
    VERCAPORG = models.TextField('验资(审计)机构', null=True, blank=True)
    CONDATE = models.DateField('出资日期', null=True, blank=True)
    updated = models.DateTimeField('更新时间', null=True, blank=True)

    class Meta:
        verbose_name = '分期实缴信息'
        verbose_name_plural = '分期实缴信息'
        app_label = "companyplus"
        db_table = 'company_stock_contri_fact'

    def __unicode__(self):
        return u'%s-分期实缴-%s' % (self.entid, self.id)

    def _currency(self):
        the_currency = list(ELCA04.objects.filter(code=self.CURRENCY_id).values_list("name", flat=True))
        if len(the_currency):
            return the_currency[0]
        return ""

    _currency.short_description = "币种"

    CURRENCY = property(_currency)


# 分期认缴信息
class ELStockContriShould(EntIdable):
    personid = models.IntegerField('关联个人id', null=True, blank=True, default=0)
    entid_inv = models.IntegerField('关联企业id', null=True, blank=True, default=0)
    SC_ID = models.CharField('认缴ID', max_length=50, null=True, blank=True)
    CONTREMNO = models.IntegerField('出资期次', null=True, blank=True)
    CONTO = models.DateField('出资期限至', null=True, blank=True)
    CURSUBCONAM = models.DecimalField('本期认缴出资额', max_digits=15, decimal_places=2, null=True, blank=True, help_text='万元')
    CURSUBCONAMUSD = models.DecimalField('本期认缴出资额折万美元', max_digits=15, decimal_places=2, null=True, blank=True, help_text='万美元')
    CURRENCY_id = CrossForeignCharField('币种', max_length=3, null=True, blank=True, db_column='CURRENCY')
    CONFORM = models.CharField('出资方式', max_length=40, null=True, blank=True, choices=ca22_dict)
    updated = models.DateTimeField('更新时间', null=True, blank=True)

    class Meta:
        verbose_name = '分期认缴信息'
        verbose_name_plural = '分期认缴信息'
        app_label = "companyplus"
        db_table = 'company_stock_contri_should'

    def __unicode__(self):
        return u'%s-分期认缴-%s' % (self.entid, self.id)

    def _currency(self):
        the_currency = list(ELCA04.objects.filter(code=self.CURRENCY_id).values_list("name", flat=True))
        if len(the_currency):
            return the_currency[0]
        return ""

    _currency.short_description = "币种"

    CURRENCY = property(_currency)


# 投资人股权转让
class ELStockTransfer(EntIdable):
    entid_inv = models.IntegerField('出让企业id', null=True, blank=True, default=0)
    personid_inv = models.IntegerField('出让个人id', null=True, blank=True, default=0)
    entid_trans = models.IntegerField('受让企业id', null=True, blank=True, default=0)
    personid_trans = models.IntegerField('受让个人id', null=True, blank=True, default=0)
    ALIEN = models.CharField('受让人', max_length=100, null=True, blank=True)
    CERNO = models.CharField('证件号码', max_length=30, null=True, blank=True, db_index=True)
    CERTYPE = models.CharField('证件类型', max_length=2, null=True, blank=True, choices=cb16_dict)
    TRANAM = models.DecimalField('转让额', max_digits=15, decimal_places=2, null=True, blank=True, help_text='万元')
    TRANAMPR = models.FloatField('转让额所占比例', null=True, blank=True)
    TRANDATE = models.DateField('转让日期', null=True, blank=True)
    TRANTYPE = models.CharField('转让类型', max_length=2, null=True, blank=True, choices=cd12_dict)
    updated = models.DateTimeField('更新时间', null=True, blank=True)

    class Meta:
        verbose_name = '投资人股权转让'
        verbose_name_plural = '投资人股权转让'
        app_label = "companyplus"
        db_table = 'company_stock_transfer'

    def __unicode__(self):
        return u'股权转让-%s' % self.id


models_stock_imported = 0  # 仅供主model引用的虚变量
